/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.craft.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.prod.craft.dao.model.ProdWorkCraftProcedure;
import com.erp.prod.craft.dao.model.ProdWorkCraftProcedureCO;
import com.erp.prod.craft.service.ProdWorkCraftProcedureService;
import com.erp.prod.procedure.dao.model.ProdWorkProcedure;
import com.erp.prod.procedure.dao.model.ProdWorkProcedureCO;
import com.erp.prod.procedure.service.ProdWorkProcedureResourceService;
import com.erp.prod.procedure.service.ProdWorkProcedureService;
import com.erp.prod.resource.service.ProdResourceService;
import com.erp.prod.workcenter.dao.model.ProdWorkCenter;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterCO;
import com.erp.prod.workcenter.service.ProdWorkCenterService;

@Controller
@RequestMapping("/web/prodWorkCraftProcedure")
public class ProdWorkCraftProcedureWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdWorkCraftProcedureWebController.class);
    
    //服务层注入
    @Autowired
    private ProdWorkCraftProcedureService prodWorkCraftProcedureService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private ProdResourceService prodResourceService;
    @Autowired
    private ProdWorkCenterService prodWorkCenterService;
    @Autowired
    private ProdWorkProcedureService prodWorkProcedureService;
    @Autowired
    private ProdWorkProcedureResourceService prodWorkProcedureResourceService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdWorkCraftProcedureList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-04 18:25:17
     * @author 
     * @param pages
     * @param prodWorkCraftProcedureCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCraftProcedureList")
    public String getProdWorkCraftProcedureList(Pages pages, ProdWorkCraftProcedureCO prodWorkCraftProcedureCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<ProdWorkCraftProcedure> prodWorkCraftProcedureList = this.prodWorkCraftProcedureService.getProcedureListByCraftCode(pages, prodWorkCraftProcedureCO);
        
        //获取工作重心列表
        ProdWorkCenterCO prodWorkCenterCO = new ProdWorkCenterCO();
        prodWorkCenterCO.setStatus("Y");
        prodWorkCenterCO.setApproveStatus("APPROVE");
        List<ProdWorkCenter> prodWorkCenterList = this.prodWorkCenterService.getDataObjects(prodWorkCenterCO);
        
        //循环获取工作中心名
        for(ProdWorkCraftProcedure prodWorkCraftProcedure: prodWorkCraftProcedureList) {
            for(ProdWorkCenter prodWorkCenter: prodWorkCenterList) {
                if(prodWorkCraftProcedure.getWorkCenterCode().equals(prodWorkCenter.getWorkCenterCode())) {
                    prodWorkCraftProcedure.setWorkCenterName(prodWorkCenter.getWorkCenterName());
                    break;
                }
            }
            
            //获取工序信息
            ProdWorkProcedure prodWorkProcedure = this.prodWorkProcedureService.getDataObject(prodWorkCraftProcedure.getProcedureCode());
            prodWorkCraftProcedure.setProcedureName(prodWorkProcedure.getProcedureName());
        }
        
        //获取单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        
        //页面属性设置
        model.addAttribute("prodWorkCraftProcedureList", prodWorkCraftProcedureList);
        model.addAttribute("pages", pages);
        model.addAttribute("materialUnitMap", materialUnitMap);
        
        return "prodWorkCraft/tab/lineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-04 18:25:17
     * @author 
     * @param prodWorkCraftProcedure
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkCraftProcedure")
    public String getProdWorkCraftProcedure(ProdWorkCraftProcedure prodWorkCraftProcedure, Model model) {
        //查询要编辑的数据
        if(prodWorkCraftProcedure!=null&&prodWorkCraftProcedure.getCraftProcedureId()!=null) {
            prodWorkCraftProcedure = this.prodWorkCraftProcedureService.getDataObject(prodWorkCraftProcedure.getCraftProcedureId());
        }
        
//        //获取资源
//        ProdResourceCO prodResourceCO = new ProdResourceCO();
//        prodResourceCO.setStatus("Y");
//        prodResourceCO.setApproveStatus("APPROVE");
//        List<ProdResource> prodResourceList = this.prodResourceService.getDataObjects(prodResourceCO);
        
        //获取工序
        ProdWorkProcedureCO prodWorkProcedureCO = new ProdWorkProcedureCO();
        prodWorkProcedureCO.setStatus("Y");
        prodWorkProcedureCO.setApproveStatus("APPROVE");
        List<ProdWorkProcedure> prodWorkProcedureList = this.prodWorkProcedureService.getDataObjects(prodWorkProcedureCO);
        
        //获取工作中心列表
        ProdWorkCenterCO prodWorkCenterCO = new ProdWorkCenterCO();
        prodWorkCenterCO.setStatus("Y");
        prodWorkCenterCO.setApproveStatus("APPROVE");
        List<ProdWorkCenter> prodWorkCenterList = this.prodWorkCenterService.getDataObjects(prodWorkCenterCO);
        
        //获取单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        
        //页面属性设置
        model.addAttribute("prodWorkCraftProcedure", prodWorkCraftProcedure);
        model.addAttribute("prodWorkCenterList", prodWorkCenterList);
        model.addAttribute("materialUnitMap", materialUnitMap);
        model.addAttribute("prodWorkProcedureList", prodWorkProcedureList);
        
        return "prodWorkCraft/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-04 18:25:17
     * @author 
     * @param prodWorkCraftProcedure
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdWorkCraftProcedure")
    @ResponseBody
    public String editProdWorkCraftProcedure(@Valid ProdWorkCraftProcedure prodWorkCraftProcedure, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //对当前编辑的对象初始化默认的字段
            if(prodWorkCraftProcedure.getCraftProcedureId()==null) {
                prodWorkCraftProcedure.setCraftProcedureCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.prodWorkCraftProcedureService.insertOrUpdateDataObject(prodWorkCraftProcedure);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-04 18:25:17
     * @author 
     * @param prodWorkCraftProcedure
     * @return String
     *
     */
    @RequestMapping("deleteProdWorkCraftProcedure")
    @ResponseBody
    public String deleteProdWorkCraftProcedure(ProdWorkCraftProcedure prodWorkCraftProcedure, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(prodWorkCraftProcedure!=null&&prodWorkCraftProcedure.getCraftProcedureId()!=null) {
                //删除数据
                this.prodWorkCraftProcedureService.deleteDataObject(prodWorkCraftProcedure);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }

}
